﻿using System.Linq;
using System.Reflection;
using Ninject;
using Ninject.Components;
using Ninject.Selection.Heuristics;

namespace SportStore.Core.IoC
{
    public class DefaultPropertyInjection : NinjectComponent, IInjectionHeuristic
    {
        #region Constants and Fields

        private readonly IKernel kernel;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="DefaultPropertyInjection"/> class.
        /// </summary>
        public DefaultPropertyInjection(IKernel kernel)
        {
            this.kernel = kernel;
        }

        #endregion

        #region Implemented Interfaces

        #region IInjectionHeuristic

        /// <summary>
        /// Returns a value indicating whether the specified member should be injected.
        /// </summary>
        public bool ShouldInject(MemberInfo member)
        {
            if (member.MemberType == MemberTypes.Property)
            {
                var property = member as PropertyInfo;

                if (property != null && property.GetSetMethod() != null)
                {
                    if (this.kernel.GetBindings(property.PropertyType).Any())
                    {
                        return true;
                    }
                }
            }

            return false;
        }

        #endregion

        #endregion
    }
}
